package com.frank.synchronizeProblem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author 小石潭记
 * @date 2021/8/25 8:56
 * @Description: ${todo}
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

//    @Transactional
    public synchronized void addEmployee() {
        // 查出ID为1的记录，然后每次将年龄增加一
        add();
    }

    @Transactional
    public void add() {
        Employee employee = employeeRepository.findById(1L).get();
        System.out.println(employee);
        Integer age = employee.getAge();
        employee.setAge(age + 1);
        employeeRepository.save(employee);
    }

    @Transactional
    public void test() {
        // 查出ID为2的记录，然后每次将年龄增加一
        Employee employee = employeeRepository.findById(2L).get();
        System.out.println(employee);
        Integer age = employee.getAge();
        employee.setAge(age + 1);
        employeeRepository.save(employee);
    }

}

